package com.wisdytech.linkdcs.equipLifeCycle.malfunction.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MConfirmRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MProcessRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.IMConfirmRecordService;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.IMProcessRecordService;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.util.Date;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dailibo
 * @since 2018-06-11
 */
@Controller
@RequestMapping("/equipLifeCycle/malfunction/confirm")
public class MConfirmRecordController {

	
	private Logger logger =LoggerFactory.getLogger(getClass());
	@Autowired
	private IMConfirmRecordService iMConfirmRecordService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	
	@RequestMapping("/index")
	public String recoreIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
		return "lifeCycle/equipLifeCycle/malfunction/confirm";
	}
	
	@RequestMapping("/addConfirm/add.mvc")
	@ResponseBody
	public StandardResult confirm(MConfirmRecord pReocrd) {
		
		SysUser user =BaseUtil.getCurrentUser();
		
		//确认密码是否合格
		String md5Password=new Md5Hash(pReocrd.getPassword()).toString().toUpperCase();
		if(!user.getPassword().equals(md5Password)) {
			throw new ServiceException("密码错误");
		}
		
		
		pReocrd.setConfirmUser(user.getUsername());
		pReocrd.setConfirmDate(new Date());
		return  iMConfirmRecordService.confirmRecord(pReocrd, user.getUsername())?
				StandardResult.getSuccessResult():
					StandardResult.getFailResult("保存失败");
	}
	
	/**
	 * 加入案例库
	 * @param pkId
	 * @return
	 */
	@RequestMapping("/addDemoResouse/add.mvc")
	@ResponseBody
	public StandardResult applyReturnFactory(String pkId) {
		iMConfirmRecordService.addDemoResouse(pkId);
		return	StandardResult.getSuccessResult();
	}



}

